package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.Emp;
import com.erp.bean.Order;
import com.erp.service.CommodityService;
import com.erp.service.EmpService;
import com.erp.service.OrderService;
import com.erp.util.factroy.ObjectFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lpy
 * 修改订单控制器
 */
@WebServlet("/orderedit.action")
public class OrderEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = ObjectFactory.getServiceInstance("com.erp.service.impl.OrderServiceImpl",OrderService.class);
        EmpService empService = ObjectFactory.getServiceInstance("com.erp.service.impl.EmpServiceImpl",EmpService.class);
        CommodityService commodityService = ObjectFactory.getServiceInstance("com.erp.service.impl.CommodityServiceImpl",CommodityService.class);
        String oid = req.getParameter("oid");
        String type = req.getParameter("type");
        Order order = orderService.findById(oid);
        if (type.equals("1")){
            Emp emp = empService.findById(order.getEid());
            List<Commodity> commodity = commodityService.findAll();
            req.setAttribute("order",order);
            req.setAttribute("commodity",commodity);
            req.setAttribute("emp",emp);
            req.getRequestDispatcher("orderedit.jsp").forward(req,resp);
        }else if (type.equals("2")){
            //获取表单提交的数据
            order.setGid(Integer.valueOf(req.getParameter("cname")));
            order.setCount(Integer.valueOf(req.getParameter("count")));
            //执行修改订单
            orderService.update(order);
            //转发到订单列表
            req.getRequestDispatcher("orderlist.action").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
